<?php
/**
 * Created by Pako
 * Date: 2019/5/15
 * Time: 21:57
 */

namespace App\Repositories\Ad;


use App\Models\Ad\AdPosition;
use App\Repositories\Repository;

class PositionRepository extends Repository
{

    public function index()
    {
        $postions = AdPosition::query()->orderBy('created_at', 'desc')
            ->paginate($this->page_size, ['*'], 'page', $this->page);
        return $this->respondWith($postions);
    }

    public function store($data)
    {
        try {
            AdPosition::query()->create($data);
            app('phpredis')->delete('redis_ad_position');
            return $this->respondSuccess();
        } catch (\Exception $e) {
            return $this->respondError(10110);
        }
    }

    public function save($data)
    {
        try {
            $ad = AdPosition::query()->find($data['position_id']);
            $ad->fill($data)->save();
            app('phpredis')->delete('redis_ad_position');
            return $this->respondSuccess();
        } catch (\Exception $e) {
            return $this->respondError(10108);
        }
    }

    public function del($position_id)
    {
        try {
            AdPosition::query()->where('position_id', $position_id)->delete();
            app('phpredis')->delete('redis_ad_position');
            return $this->respondSuccess();
        } catch (\Exception $e) {
            return $this->respondError(10108);
        }
    }

}